package ai.people.core.security.extractor;

import ai.people.netmon.utils.CookieUtil;
import org.springframework.core.annotation.Order;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * Cookie提取器
 *
 * @author yuanqinglong
 * @date 2022/7/11 13:03
 */
@Order(1)
public class CookieAuthorizationValueExtractor implements TokenValueExtractor {


    /**
     * 提取令牌
     *
     * @param request 请求
     * @return {@link String}
     */
    @Override
    public String extractToken(HttpServletRequest request) {
        // 从登录写入的cookie中获取token
        Cookie cookie = CookieUtil.getCookie(request, "Authorization");
        // cookie中获取不到 从请求头中获取
        return Optional.ofNullable(cookie).map(Cookie::getValue).orElse(null);
    }

}
